<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">

      <el-form-item prop="girdId" label="网格：">
        <el-select v-model="dataForm.girdId" placeholder="请选择网格" @change="girdChange" >
          <el-option v-for="item in girdList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="gridOfVillageId" label="自然村：">
        <el-select v-model="dataForm.gridOfVillageId" placeholder="请选择自然村" @change="gridOfVillageChange">
          <el-option v-for="item in gridOfVillageList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="locationType" label="类型" class="role-list">
        <ren-select v-model="dataForm.locationType" dict-type="dimension_type" placeholder="类型"></ren-select>
      </el-form-item>

      <el-form-item label="门牌号" prop="houseNum">
        <el-input v-model="dataForm.houseNum" placeholder="门牌号"></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import {ElMessage, ElNotification} from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const girdList = ref();
const gridOfVillageList = ref();

const dataForm = reactive({
  id: "",
  girdId: null,
  girdName: null,
  gridOfVillageId: null,
  gridOfVillageName: null,
  locationType: "0",
  locationMarkId: null,
  houseNum: null,
  remark: null,
});

const rules = ref({
  // girdId: [{ required: true, message: t("validate.required"), trigger: "change" }],
  // gridOfVillageId: [{ required: true, message: t("validate.required"), trigger: "change" }],
  // houseNum: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

const getGridList = () => {
  baseService.get(`/sys/dept/list`).then((res) => {
    girdList.value = res.data[0].children;
    if(dataForm.girdId != "") {
      gridOfVillageList.value = girdList.value.filter((item: { id: any; }) => item.id === dataForm.girdId )[0].children
    }
  });
};

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  } else {
    getGridList();
  }
};

const girdChange = (e: any) => {
  dataForm.gridOfVillageId = ""
  gridOfVillageList.value = [];
  dataForm.girdName = girdList.value.filter((item: { id: any; }) => item.id === e)[0].name;
  gridOfVillageList.value = girdList.value.filter((item: { id: any; }) => item.id === e)[0].children
};

const gridOfVillageChange = (e: any) => {
  dataForm.gridOfVillageName = gridOfVillageList.value.filter((item: { id: any; }) => item.id === e)[0].name;
};




// 获取信息
const getInfo = (id: number) => {
  baseService.get("/clcLocationMark/clclocationmark/" + id).then((res) => {
    Object.assign(dataForm, res.data);
    getGridList();
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/clcLocationMark/clclocationmark", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
